#!/bin/bash

check_env () {
  if [ -z "$1" ];
  then
    echo "miss env $2"
    exit 1
  fi
}

check_env "$HOST_OF_API" "HOST_OF_API"
check_env "$HOST_OF_WEBSOCKET" "HOST_OF_WEBSOCKET"
check_env "$HOST_OF_KMX" "HOST_OF_KMX"
check_env "$HOST_OF_KMX_REAL_TIME" "HOST_OF_KMX_REAL_TIME"
check_env "$HOST_OF_SPE_API" "HOST_OF_SPE_API"
check_env "$HOST_OF_KMX_TSDB_QUERY_URL" "KMX_TSDB_QUERY_URL"

check_env "$HOST_OF_API_WX" "HOST_OF_API_WX"
check_env "$HOST_OF_KMX_WX" "HOST_OF_KMX_WX"
check_env "$HOST_OF_KMX_REAL_TIME_WX" "HOST_OF_KMX_REAL_TIME_WX"
check_env "$HOST_OF_SPE_API_WX" "HOST_OF_SPE_API_WX"

[[ -z $APP_TITLE ]] || /bin/sed -i "s@<title>.*</title>@<title>${APP_TITLE}</title>@" \
  /home/node/app/public/index.html

/bin/sed -i "s@__SERVICE_API_PLACEHOLDER__@${HOST_OF_API}@" /home/node/app/public/config.js
/bin/sed -i "s@__WEBSOCKET_API_PLACEHOLDER__@${HOST_OF_WEBSOCKET}@" /home/node/app/public/config.js
/bin/sed -i "s@__KMX_API_PLACEHOLDER__@${HOST_OF_KMX}@" /home/node/app/public/config.js
/bin/sed -i "s@__KMX_REAL_TIME_API_PLACEHOLDER__@${HOST_OF_KMX_REAL_TIME}@" /home/node/app/public/config.js
/bin/sed -i "s@__KMX_TSDB_QUERY_URL_PLACEHOLDER__@${HOST_OF_KMX_TSDB_QUERY_URL}@" /home/node/app/public/config.js
/bin/sed -i "s@__SPE_API_PLACEHOLDER__@${HOST_OF_SPE_API}@" /home/node/app/public/config.js

/bin/sed -i "s@__WXSERVICE_API_PLACEHOLDER__@${HOST_OF_API_WX}@" /home/node/app/public/config.js
/bin/sed -i "s@__WXKMX_API_PLACEHOLDER__@${HOST_OF_KMX_WX}@" /home/node/app/public/config.js
/bin/sed -i "s@__WXKMX_REAL_TIME_API_PLACEHOLDER__@${HOST_OF_KMX_REAL_TIME_WX}@" /home/node/app/public/config.js
/bin/sed -i "s@__WXSPE_API_PLACEHOLDER__@${HOST_OF_SPE_API_WX}@" /home/node/app/public/config.js

node server.js
